﻿#include "AF_Bundle.h"

namespace AF
{
	Bundle::Bundle()
	{
	}

	Bundle::~Bundle()
	{
		Clean();
	}

	bool Bundle::PutBoolean(_STRING strKey, bool b)
	{
		bool isReturn;

		bool* _pb = AF_NEW(bool);
		*_pb = b;

		if(FindArg(strKey).pArg == NULL)
			isReturn = PutArg(strKey, (Pointer)_pb, ARG_BOOLEAN);
		else
			isReturn = ChangeArg(strKey, (Pointer)_pb, ARG_BOOLEAN);

		if(!isReturn)
		{
			AF_ERRMARK();
		}

		return isReturn;
	}

	bool Bundle::PutInteger(_STRING strKey, int n)
	{
		bool isReturn;

		int* _pn = AF_NEW(int);
		*_pn = n;

		if(FindArg(strKey).pArg == NULL)
			isReturn = PutArg(strKey, (Pointer)_pn, ARG_INTEGER);
		else
			isReturn = ChangeArg(strKey, (Pointer)_pn, ARG_INTEGER);

		if(!isReturn)
		{
			AF_ERRMARK();
		}

		return isReturn;
	}

	bool Bundle::PutLong(_STRING strKey, long l)
	{
		bool isReturn;

		long* _pl = AF_NEW(long);
		*_pl = l;

		if(FindArg(strKey).pArg == NULL)
			isReturn = PutArg(strKey, (Pointer)_pl, ARG_LONG);
		else
			isReturn = ChangeArg(strKey, (Pointer)_pl, ARG_LONG);

		if(!isReturn)
		{
			AF_ERRMARK();
		}

		return isReturn;
	}

	bool Bundle::PutFloat(_STRING strKey, float f)
	{
		bool isReturn;

		float* _pf = AF_NEW(float);
		*_pf = f;

		if(FindArg(strKey).pArg == NULL)
			isReturn = PutArg(strKey, (Pointer)_pf, ARG_FLORT);
		else
			isReturn = ChangeArg(strKey, (Pointer)_pf, ARG_FLORT);

		if(!isReturn)
		{
			AF_ERRMARK();
		}

		return isReturn;
	}

	bool Bundle::PutDouble(_STRING strKey, double d)
	{
		bool isReturn;

		double* _pd = AF_NEW(double);
		*_pd = d;

		if(FindArg(strKey).pArg == NULL)
			isReturn = PutArg(strKey, (Pointer)_pd, ARG_DOUBLE);
		else
			isReturn = ChangeArg(strKey, (Pointer)_pd, ARG_DOUBLE);

		if(!isReturn)
		{
			AF_ERRMARK();
		}

		return isReturn;
	}

	bool Bundle::PutChars(_STRING strKey, char* sz)
	{
		bool isReturn;

		char* _psz = AF_NEW_ARY(char, _STRLEN(sz));
		_STRCPY(_psz, sz);

		if(FindArg(strKey).pArg == NULL)
			isReturn = PutArg(strKey, (Pointer)_psz, ARG_CHARS);
		else
			isReturn = ChangeArg(strKey, (Pointer)_psz, ARG_CHARS);

		if(!isReturn)
		{
			AF_ERRMARK();
		}

		return isReturn;
	}

	bool Bundle::PutString(_STRING strKey, _STRING str)
	{
		bool isReturn;

		_STRING* _pstr = AF_NEW(_STRING);
		_pstr->assign(str);

		if(FindArg(strKey).pArg == NULL)
			isReturn = PutArg(strKey, (Pointer)_pstr, ARG_STRING);
		else
			isReturn = ChangeArg(strKey, (Pointer)_pstr, ARG_STRING);

		if(!isReturn)
		{
			AF_ERRMARK();
		}

		return isReturn;
	}

	bool Bundle::PutPointer(_STRING strKey, Pointer p)
	{
		bool isReturn;

		Pointer* _pp = AF_NEW(Pointer);
		*_pp = p;

		if(FindArg(strKey).pArg == NULL)
			isReturn = PutArg(strKey, (Pointer)_pp, ARG_POINTER);
		else
			isReturn = ChangeArg(strKey, (Pointer)_pp, ARG_POINTER);

		if(!isReturn)
		{
			AF_ERRMARK();
		}

		return isReturn;
	}

	bool Bundle::GetBoolean(_STRING strKey)
	{
		m_ArgTemp = FindArg(strKey);
		if(m_ArgTemp.kind != ARG_BOOLEAN)
		{
			return false;
		}

		return *(bool*)(m_ArgTemp.pArg);
	}

	int Bundle::GetInteger(_STRING strKey)
	{
		m_ArgTemp = FindArg(strKey);
		if(m_ArgTemp.kind != ARG_INTEGER)
		{
			return 0;
		}

		return *(int*)(m_ArgTemp.pArg);
	}

	long Bundle::GetLong(_STRING strKey)
	{
		m_ArgTemp = FindArg(strKey);
		if(m_ArgTemp.kind != ARG_LONG)
		{
			return 0;
		}

		return *(long*)(m_ArgTemp.pArg);
	}

	float Bundle::GetFloat(_STRING strKey)
	{
		m_ArgTemp = FindArg(strKey);
		if(m_ArgTemp.kind != ARG_FLORT)
		{
			return 0;
		}

		return *(float*)(m_ArgTemp.pArg);
	}

	double Bundle::GetDouble(_STRING strKey)
	{
		m_ArgTemp = FindArg(strKey);
		if(m_ArgTemp.kind != ARG_DOUBLE)
		{
			return 0;
		}

		return *(double*)(m_ArgTemp.pArg);
	}

	char* Bundle::GetChars(_STRING strKey)
	{
		m_ArgTemp = FindArg(strKey);
		if(m_ArgTemp.kind != ARG_CHARS)
		{
			return 0;
		}

		return *(char**)(m_ArgTemp.pArg);
	}

	_STRING Bundle::GetString(_STRING strKey)
	{
		m_ArgTemp = FindArg(strKey);
		if(m_ArgTemp.kind != ARG_STRING)
		{
			return "";
		}

		return *(_STRING*)(m_ArgTemp.pArg);
	}

	Pointer Bundle::GetPointer(_STRING strKey)
	{
		m_ArgTemp = FindArg(strKey);
		if(m_ArgTemp.kind != ARG_POINTER)
		{
			return 0;
		}

		return *(Pointer*)(m_ArgTemp.pArg);
	}

	void Bundle::Clean()
	{
		int _nLen = m_tabArgs.GetCount();

		if(_nLen > 0)
		{
			Arg* _pArgs = AF_NEW_ARY(Arg, _nLen);

			m_tabArgs.Clean(_pArgs);

			for(int _i = 0; _i < _nLen; _i++)
			{
				AF_DELETE(_pArgs->pArg);
			}

			AF_DELETE_ARY(_pArgs);
		}
	}

	Bundle::Arg& Bundle::FindArg(_STRING strKey)
	{
		return m_tabArgs.Get(strKey);
	}

	bool Bundle::PutArg(_STRING strKey, Pointer pArg, ArgKind kind)
	{
		Arg _NewArg;
		_NewArg.pArg = pArg;
		_NewArg.kind = kind;

		return m_tabArgs.Put(strKey, _NewArg);
	}

	bool Bundle::ChangeArg(_STRING strKey, Pointer pArg, ArgKind kind)
	{
		Arg& _arg = FindArg(strKey);
		if(_arg.kind != kind)
		{
			return false;
		}

		AF_DELETE(_arg.pArg);
		_arg.pArg = pArg;

		return true;
	}
}
